//
//  YZSegmentController.m
//  kuaiyi
//
//  Created by wyman on 16/5/6.
//
//

#import "YZSegmentController.h"
#import "YZNetWorkTool.h"
#import "YZManagerModels.h"
#import <MJRefresh.h>
@interface YZSegmentController ()
@property (nonatomic, strong) NSMutableArray *dates;
@end

@implementation YZSegmentController

- (void)viewDidLoad {
    [super viewDidLoad];
    
   
    
//    UISearchBar *bar = [[UISearchBar alloc]initWithFrame:CGRectMake(100, -30, 200, 100)];
//    
//   
//    [self.navigationController.navigationBar addSubview:bar];
    
    
     [self getNetDates];
     [self topRefresh];
    
   
    
   
    
//    [self bottomRefresh];

    
}
//上拉刷新

-(void)topRefresh{
    
    __weak UITableView *tableView = self.tableView;
    
    __weak YZSegmentController *weakself = self;
    //注册上啦刷新
    MJRefreshNormalHeader *normal = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        NSString *urlString = @"http://iosapi.itcast.cn/doctor/searchCI3List.json.php";
        NSDictionary *parameters = @{
                                     @"page":@2,
                                     @"page_size":@15,
                                     @"ci1_id":self.cil_id,
                                     @"keyword":@"",
                                     };
        
//        if ([self.cil_id  isEqual: @2]) {
//            
//            [tableView.mj_header endRefreshing];
//
//            return ;
//        }
        
        [[YZNetWorkTool sharedNetWorkTool]POST:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *responseObject) {
            
            if ([responseObject[@"data"] isKindOfClass: [NSNull class]]) {
                [tableView.mj_header endRefreshing];
               
                return;
            }
            
            NSArray *dateArr = responseObject[@"data"];
            
            NSMutableArray *dates = [YZManagerModels arrWithArray:dateArr];
            
            [dates addObjectsFromArray:weakself.dates];
            
            
            weakself.dates = dates;
            
            [tableView reloadData];
            [tableView.mj_header endRefreshing];
            //        NSLog(@"%@",dateArr);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
        }];
        
    }];
    
    
    tableView.mj_header = normal;
   
}

/**
 *  下拉刷新
 */

-(void)bottomRefresh{

    
    __weak YZSegmentController *weakself = self;
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        

        //注册上啦刷新
     
            
            NSString *urlString = @"http://iosapi.itcast.cn/doctor/searchCI3List.json.php";
            NSDictionary *parameters = @{
                                         @"page":@5,
                                         @"page_size":@15,
                                         @"ci1_id":self.cil_id,
                                         @"keyword":@"",
                                         };
            
            [[YZNetWorkTool sharedNetWorkTool]POST:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *responseObject) {
                
                NSArray *dateArr = responseObject[@"data"];
                
                NSMutableArray *dates = [YZManagerModels arrWithArray:dateArr];
                
                //            [dates addObjectsFromArray:weakself.dates];
                
                
                [weakself.dates addObjectsFromArray:dates];
                //            weakself.dates = dates;
                
                [self.tableView reloadData];
                [self.tableView.mj_footer endRefreshing];
                //        NSLog(@"%@",dateArr);
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                
            }];
            
        }];
        

    


}


//MARK: 加载下拉数据
-(void)loadbottom{

    NSString *urlString = @"http://iosapi.itcast.cn/doctor/searchCI3List.json.php";
    NSDictionary *parameters = @{
                                 @"page":@1,
                                 @"page_size":@15,
                                 @"ci1_id":self.cil_id,
                                 @"keyword":@"",
                                 };
    
    [[YZNetWorkTool sharedNetWorkTool]POST:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *responseObject) {
        
      
        NSArray *dateArr = responseObject[@"data"];
        
        NSMutableArray *dates = [YZManagerModels arrWithArray:dateArr];
        
        self.dates = dates;
        [self.tableView reloadData];
        
        [self.tableView.mj_footer endRefreshing];
    
        //        NSLog(@"%@",dateArr);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];

}




-(void)getNetDates{
    
    
    
    NSString *urlString = @"http://iosapi.itcast.cn/doctor/searchCI3List.json.php";
    NSDictionary *parameters = @{
                                 @"page":@1,
                                 @"page_size":@15,
                                 @"ci1_id":self.cil_id,
                                 @"keyword":@"",
                                 };
    
    [[YZNetWorkTool sharedNetWorkTool]POST:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *responseObject) {
        
        NSArray *dateArr = responseObject[@"data"];
        
        NSMutableArray *dates = [YZManagerModels arrWithArray:dateArr];
        
        self.dates = dates;
        [self.tableView reloadData];

//        NSLog(@"%@",dateArr);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.dates.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    NSString *ID = @"type";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        
    }
    
    cell.textLabel.text =((YZManagerModels *)self.dates[indexPath.row]).ci3_name;
    cell.textLabel.textColor = [UIColor blackColor];
    cell.textLabel.textAlignment = NSTextAlignmentLeft;
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [self.navigationController popViewControllerAnimated:YES];
    
    self.illTypeblock(((YZManagerModels *)self.dates[indexPath.row]).ci3_name);
    
}

@end
